/**
 *
 * @author
 *
 */
class GameStage extends egret.DisplayObjectContainer {

    private _stageBg: egret.Bitmap;
    private _cannon: Cannon;
    private _heroGroup: Array<Array<HeroBase>> = [[],[],[]];
    private _enemyGroup: Array<Enemy> = [];

    private _lastSceneIndex: number = -1;

    private _zoomStage(name: string): void {

        var infoTable: Object = {
            'cannon' : { scaleX: 2,scaleY: 2,x: 1500,y: -220 },
            'fight'  : { scaleX: 2,scaleY: 2,x: -150, y: -220 },
            'all'    : { scaleX: 0.9,scaleY: 0.9,x: 600, y: 250 }
        };

        egret.Tween.get(this).to(infoTable[name],1000,egret.Ease.cubicInOut);
    }

    private _opEnemy(opName: string): void {

        var i: number;

        for(i = 0;i < this._enemyGroup.length;++i) {
            this._enemyGroup[i][opName].call(this._enemyGroup[i]);
        }
    }

    private _showEnemy(): void {

        var tween: egret.Tween = egret.Tween.get(this),
            that = this,
            i: number;

        for(i = 0;i < this._enemyGroup.length;++i) {
            (function() {
                var j: number = i;
                tween = tween.call(function() {
                    that._enemyGroup[j].show();
                }).wait(300);
            }());
        }
    }

    private _playHero(scene: number,index: number,callback?: Function): void {

        var that = this,
            hero: HeroBase = this._heroGroup[scene][index];

        this.addChildAt(hero,this.getChildIndex(this._cannon));

        egret.Tween.get(this).call(function() {
            this._zoomStage('cannon');

        }).wait(1500).call(function() {

            that._cannon.fireCannon(function() {

                that._zoomStage('all');

                hero.play(function(status: string) {

                    if(status === 'fall') {
                        that._zoomStage('fight');
                    } else if (status === 'hurt') {
                        that._opEnemy('hurt');
                    } else if (status === 'die') {
                        that._opEnemy('die');
                    } else if (status === 'done') {
                        callback && callback.call(that);
                    } else {
                        // Do nothing
                    }
                });
            });
        });
    }

    private _resetScene(): void {

        if(this._lastSceneIndex !== -1) {
            this.removeChild(this._heroGroup[this._lastSceneIndex][0]);
            this.removeChild(this._heroGroup[this._lastSceneIndex][1]);
        }

        this._opEnemy('hide');
    }

    private _playScene(index: number): void {

        this._lastSceneIndex = index;

        egret.Tween.get(this).call(function() {
            this._zoomStage('fight');
        }).wait(2000).call(function() {
            this._showEnemy();
        }).wait(5000).call(function() {
            EventBus.trigger('GAME_FIRE_READY');
            EventBus.one('OP_FIRE_FIRST',function() {
                this._playHero(index,0,function() {
                    EventBus.trigger('GAME_HALF_DONE');
                    EventBus.one('OP_FIRE_SECOND',function() {
                        this._playHero(index,1,function() {
                            EventBus.trigger('GAME_ALL_DONE');
                        });
                    },this);
                });
            },this);
        });
    }

    public constructor() {
        super();

        var i: number;

        this.anchorOffsetX = 1200;
        this.anchorOffsetY = 750;
        this.width  = 2400;
        this.height = 1500;
        this.scaleX = 0.6;
        this.scaleY = 0.6;
        this.x = 600;
        this.y = 375;

        this._heroGroup[0][0] = new HeroGuanyu();
        this._heroGroup[0][1] = new HeroXiaoxiao();
        this._heroGroup[1][0] = new HeroYanhou();
        this._heroGroup[1][1] = new HeroLong();
        this._heroGroup[2][0] = new HeroShengqishi();
        this._heroGroup[2][1] = new HeroZhugeliang();

        this._stageBg = new egret.Bitmap();
        this._stageBg.texture = RES.getRes('page/stage-bg.jpg');
        this._stageBg.x = 0;
        this._stageBg.y = 0;
        this.addChild(this._stageBg);

//        this._enemyGroup.push(new Enemy('long',1830,1150));
        this._enemyGroup.push(new Enemy('yezhu',1560,1150));
        this._enemyGroup.push(new Enemy('shuyao', 1600, 1150));
        this._enemyGroup.push(new Enemy('shachong',1660,1150));
        this._enemyGroup.push(new Enemy('kulou',1700,1150));
        this._enemyGroup.push(new Enemy('shuyao',1740,1150));
        this._enemyGroup.push(new Enemy('shachong',1770,1150));
        this._enemyGroup.push(new Enemy('kulou',1820,1150));
        this._enemyGroup.push(new Enemy('shuyao',1740,1150));

        for (i = 0; i < this._enemyGroup.length ;++i) {
            this.addChild(this._enemyGroup[i]);
        }

        this._cannon = new Cannon();
        this._cannon.x = 400;
        this._cannon.y = 740;
        this.addChild(this._cannon);

        EventBus.on('OP_PLAY_SCENE',function(id: number) {
            this._resetScene();
            this._playScene(id);
        },this);

        egret.Ticker.getInstance().register(function (advancedTime) {
            dragonBones.WorldClock.clock.advanceTime(advancedTime / 1000);
        }, this);
    }
}
